package cn.touna.jss.modules.third.geo.component;

import cn.touna.jss.common.properties.disconf.GeoProperties;
import com.alibaba.fastjson.JSONObject;
import cn.touna.jss.modules.third.geo.util.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class GeoDataResult {
    private static final Logger LOGGER = LoggerFactory.getLogger(GeoDataResult.class);

    @Autowired
    private GeoProperties geoProperties;

    // 构造客户端(线程安全)
    public static final Client client = new Client();  // 如果接入只是一个账号的话那么该类的构造只需在启动的时候构造一次即可

    private static final String B7 = "B7";//手机号码身份证号姓名验证接口

    private static final String A2 = "A2";//手机号码姓名验证接口

    private static final String A3 = "A3";//手机号码在网时长查询接口


    /**
     * 初始化client
     */
    public void setClient() {
        client.setServer(geoProperties.getServer());
        client.setEncrypted(Integer.parseInt(geoProperties.getEncrypted()));
        client.setEncryptionType(geoProperties.getEncryptionType());
        client.setEncryptionKey(geoProperties.getEncryptionKey());
        client.setUsername(geoProperties.getUsername());
        client.setPassword(geoProperties.getPassword());
        client.setUno(geoProperties.getUno());
        client.setEtype(geoProperties.getEtype());
        client.setDsign(Integer.valueOf(geoProperties.getDsign()));
        client.setHttpConnectTimeout(Integer.valueOf(geoProperties.getHttpConnectTimeout()));
        client.setHttpReadTimeout(Integer.valueOf(geoProperties.getHttpReadTimeout()));
    }

   /* public String getGeoData(GeoEntity geoEntity, String interfaceType) {
        String getData = null;
        if (B7.equals(interfaceType)) {
            getData = getThreeBaseData(geoEntity.getMobile(), geoEntity.getIdCard(), geoEntity.getName());
        } else if (A2.equals(interfaceType)) {
            getData = getMobileNameCheckData(geoEntity.getMobile(), geoEntity.getName());
        } else if (A3.equals(interfaceType)) {
            getData = getMobileNumberOnlineTime(geoEntity.getMobile(), geoEntity.getIdCard(), geoEntity.getName());
        }
        return getData;
    }*/

    /**
     * 集奥-手机号码身份证号姓名验证接口
     *
     * @param mobile   手机号码
     * @param icCard 身份证号
     * @param name     姓名
     */
    public String getThreeBaseData(String mobile, String icCard, String name) {
        setClient();
        String url = geoProperties.getServer() + geoProperties.getPath();//请求地址
        Map<String, String> params = new HashMap<String, String>();
        params.put("innerIfType", B7);//接口类型：手机号码身份证号姓名验证接口
        params.put("cid", mobile);//手机号码
        params.put("idNumber", icCard);//身份证
        params.put("realName", name);//姓名
        params.put("authCode", client.rpad(geoProperties.getUno() + ":" + params.get("cid"), 32));
        // 请求数据接口返回json
        String data = null;
        try {
            data = client.getData(url, params);
        } catch (Exception e) {
            data = geoException();
            LOGGER.error("client.getData方法调用异常！", e);
        }
        return data;
    }

    /**
     * 集奥-手机号码姓名验证接口
     *
     * @param mobile 手机号码
     * @param name   姓名
     */
    public String getMobileNameCheckData(String mobile, String name) {
        setClient();
        String url = geoProperties.getServer() + geoProperties.getPath();//请求地址
        Map<String, String> params = new HashMap<String, String>();
        params.put("innerIfType", A2);//接口类型：手机号码姓名验证接口
        params.put("cid", mobile);//手机号码
        params.put("realName", name);//姓名
        params.put("authCode", client.rpad(geoProperties.getUno() + ":" + params.get("cid"), 32));
        // 请求数据接口返回json
        String data = null;
        try {
            data = client.getData(url, params);
        } catch (Exception e) {
            data = geoException();
            LOGGER.error("client.getData方法调用异常！", e);
        }
        return data;
    }

    /**
     * 集奥-手机号码在网时长查询接口
     *
     * @param mobile   手机号码
     * @param icCard 身份证号码
     * @param name     姓名
     */
    public String getMobileNumberOnlineTime(String mobile, String icCard, String name) {
        setClient();
        String url = geoProperties.getServer() + geoProperties.getPath();//请求地址
        Map<String, String> params = new HashMap<String, String>();
        params.put("innerIfType", A3);//接口类型：手机号码姓名验证接口
        params.put("cid", mobile);//手机号码
        params.put("idNumber", icCard);
        params.put("realName", name);//姓名
        params.put("authCode", client.rpad(geoProperties.getUno() + ":" + params.get("cid"), 32));
        // 请求数据接口返回json
        String data = null;
        try {
            data = client.getData(url, params);
            LOGGER.info("请求集奥接口，返回数据为{}", data);
        } catch (Exception e) {
            data = geoException();
            LOGGER.error("client.getData方法调用异常！", e);
        }
        return data;
    }

    public String geoException() {
        String json = "{\"data\":{\"ECL\":[]},\"code\":\"201\",\"msg\":\"getData()方法异常！\"}";
        return JSONObject.parseObject(json).toJSONString();
    }
}
